<template>
    <div class="tinymce-editor">
      <editor v-model="myValue" :init="init" :disabled="disabled" @onClick="onClick">
      </editor>
    </div>
  </template>
   
  <script>
  import tinymce from 'tinymce/tinymce'
  import Editor from '@tinymce/tinymce-vue'
  import 'tinymce/themes/silver/theme'
  import 'tinymce/plugins/image'
  import 'tinymce/plugins/media'
  import 'tinymce/plugins/table'
  import 'tinymce/plugins/lists'
  import 'tinymce/plugins/contextmenu'
  import 'tinymce/plugins/wordcount'
  import 'tinymce/plugins/colorpicker'
  import 'tinymce/plugins/textcolor'
  import "tinymce/plugins/preview";
  import "tinymce/plugins/code";
  import "tinymce/plugins/link";
  import "tinymce/plugins/advlist";
  import "tinymce/plugins/codesample";
  import "tinymce/plugins/hr";
  import "tinymce/plugins/fullscreen";
  import "tinymce/plugins/textpattern";
  import "tinymce/plugins/searchreplace";
  import "tinymce/plugins/autolink";
  import "tinymce/plugins/directionality";
  import "tinymce/plugins/visualblocks";
  import "tinymce/plugins/visualchars";
  import "tinymce/plugins/template";
  import "tinymce/plugins/charmap";
  import "tinymce/plugins/nonbreaking";
  import "tinymce/plugins/insertdatetime";
  import "tinymce/plugins/imagetools";
  import "tinymce/plugins/autosave";
  import "tinymce/plugins/autoresize"; 
  export default {
    components: {
      Editor
    },
    props: {
      //传入一个value，使组件支持v-model绑定
      value: {
        type: String,
        default: ''
      },
      disabled: {
        type: Boolean,
        default: false
      },
      plugins: {
        type: [String, Array],
        default:
          'preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr nonbreaking insertdatetime advlist lists wordcount imagetools textpattern autosave autoresize'
          // 'lists image media table  wordcount '
          // 'image media table link code table lists wordcount'
      },
      toolbar: {
        type: [String, Array],
        default:
        'undo redo restoredraft | \
        styleselect fontselect fontsizeselect | \
        cut copy paste pastetext | \
        forecolor backcolor bold italic underline strikethrough link codesample | \
        alignleft aligncenter alignright alignjustify outdent indent lineheight formatpainter | \
        bullist numlist | \
        blockquote subscript superscript removeformat | \
        table image media charmap hr pagebreak insertdatetime | \
        bdmap fullscreen preview | \
        toc contextmenu textcolor colorpicker'
          // 'code undo redo | cut copy paste pastetext | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists | removeformat hr'
          // 'undo redo |  formatselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat hr'
      }
    },
    data () {
      return {
        //初始化配置
        init: {
          language_url: require('@/assets/langs/zh_CN.js'),
          language: 'zh_CN',
          skin_url: 'tinymce/skins/ui/oxide',
          height: 200,
          min_height: 200,
          max_height: 200,
          object_resizing: false, //禁用表格内联样式拖拽拉伸
          table_resize_bars: false,//禁用表格单元格拖拽拉伸
          toolbar_mode:'wrap', // 设置编辑器工具栏模式，取值：floating / sliding / scrolling / wrap
          plugins: this.plugins,
          toolbar: this.toolbar,
          content_style: "p {margin: 5px 0;}", //内容样式
          fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
          font_formats: "宋体='宋体';仿宋='仿宋';微软雅黑='微软雅黑';楷体='楷体';;隶书='隶书';幼圆='幼圆';Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings",
          lineheight_formats: '0.5 0.8 1 1.2 1.5 1.75 2 2.5 3 4 5', // 设置编辑器行高，也可配置成 '12px 14px 16px 20px' 的形式
          letterspacing_formats: '0 0.5pt 1pt 1.5pt 2pt 3pt 5pt 10pt 15pt 20pt 30pt', // 设置编辑器文字间距
          branding: false,//不显示富文本支持方
          menubar: false,//禁用菜单栏  如果放开菜单栏，则把这个代码注释
          resize: false
        },
        myValue: this.value
      }
    },
    mounted () {
      tinymce.init({})
    },
    methods: {
      //添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
      //需要什么事件可以自己增加
      onClick (e) {
        this.$emit('onClick', e, tinymce)
      },
      //可以添加一些自己的自定义事件，如清空内容
      clear () {
        this.myValue = ''
      }
    },
    watch: {
      value (newValue) {
        this.myValue = newValue
      },
      myValue (newValue) {
        this.$emit('input', newValue)
      }
    }
  }
  </script>
  <style scoped>
  </style>